package com.photoeditor.demo.model.face.report.reportlogic;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.common.base.utils.DeviceUtils;
import com.photoeditor.R;
import com.photoeditor.demo.model.preference.SPDataManager;
import com.photoeditor.demo.model.subscribe.strategy.SVipStrategyManager;
import com.photoeditor.demo.model.bean.face.BaseFaceResultBean;
import com.photoeditor.demo.util.FontUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by @author winn on 2018/10/29.
 */

public abstract class BaseReportLogic implements View.OnClickListener {

//    public static int sImgOriginalHeight = DeviceUtils.dip2px(142);
//    public static int sDescriptionWidth = DeviceUtils.dip2px(144);
//    public static int sNameOringinalSize = 18;
//    public static int sDescriptionOriginalSize = 13;
//    public static int sEmptyViewHeight = DeviceUtils.dip2px(18);
//    public static float sImgRatio = 2.53521f;

    protected int mReportType;
    protected OnEventListener mOnEventListener;
    protected View mContentView;
    protected View mMaskLayout;
    private String mRecommendFunName;

    public BaseReportLogic(int type) {
        mReportType = type;
    }

    public abstract View getReportView(ViewGroup parent);

    public abstract int getReportTitleResId();

    public abstract String getFeatureType();

    public abstract boolean needScrool();

    public abstract void updateImage(String image);

    public OnEventListener getOnEventListener() {
        return mOnEventListener;
    }

    public int getReportType() {
        return mReportType;
    }

    public void setOnEventListener(OnEventListener onEventListener) {
        mOnEventListener = onEventListener;
    }

    public boolean titleBarWithoutBg() {
        return false;
    }

    @Override
    public void onClick(View view) {
    }

    public interface OnEventListener {
        void gotoHome();
        void back();
        void switchImage();
        void updateResultBean();
    }

    public abstract void onSubscribeSuccess();

    public void updateMaskLayout(){};

    public Bitmap getSaveImg() {
        Bitmap ret = null;
        if (mContentView != null) {
            mContentView.setDrawingCacheEnabled(true);
            mContentView.buildDrawingCache();
            Bitmap cache = mContentView.getDrawingCache();
            ret = cache;
            if (cache == null) {
                Bitmap bitmap = Bitmap.createBitmap(mContentView.getWidth(),
                        mContentView.getHeight(),
                        Bitmap.Config.ARGB_8888);
                Canvas canvas = new Canvas(bitmap);
                mContentView.draw(canvas);
                ret = bitmap;
            }
            //mContentView.destroyDrawingCache();
        }
        return ret;
    }
//
//    public LinearLayout getRecommendView(ViewGroup parentView, int funId) {
//        LinearLayout ret = new LinearLayout(parentView.getContext());
//        ret.setOrientation(LinearLayout.VERTICAL);
//        ViewGroup viewGroup = (ViewGroup) LayoutInflater.from(parentView.getContext())
//                .inflate(R.layout.item_face_scan, ret, false);
//        int funNameResId = R.string.report_title_baby;
//        int desId = R.string.txt_feature_baby;
//        int imgId = R.drawable.bg_funcation_time_machine;
//        switch (funId) {
//            case BaseFaceResultBean.REPORT_TYPE_FACE_AGING:
//                funNameResId = R.string.report_title_faceaging;
//                desId = R.string.txt_time_machine;
//                imgId = R.drawable.bg_funcation_time_machine;
//
//            case BaseFaceResultBean.REPORT_TYPE_EXOTIC:
//                funNameResId = R.string.exotic_blend;
//                desId = R.string.txt_exotic_blend;
//                imgId = R.drawable.bg_funcation_time_machine;
//                break;
//            case BaseFaceResultBean.REPORT_TYPE_GENDER_SWITCH:
//                funNameResId = R.string.gender_switch;
//                desId = R.string.txt_gender_switch;
//                imgId = R.drawable.bg_funcation_time_machine;
//                break;
//            default:
//                break;
//        }
//        TextView nameView = viewGroup.findViewById(R.id.tv_name);
//        nameView.setText(funNameResId);
//        TextView desTV = viewGroup.findViewById(R.id.tv_description);
//        desTV.setText(desId);
//        FontUtil.setCustomFontMedium(desTV);
//        ImageView icon = viewGroup.findViewById(R.id.img_logo);
//        icon.setImageResource(imgId);
//
//        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) icon.getLayoutParams();
//        layoutParams.width = DeviceUtils.getScreenWidthPx();
//        layoutParams.height = (int) (layoutParams.width / sImgRatio);
//        icon.setLayoutParams(layoutParams);
//        float ratio = layoutParams.height / sImgOriginalHeight;
//        ratio = ratio == 0 ? 1 : ratio;
//
//        View emptyView = viewGroup.findViewById(R.id.empty_view);
//        RelativeLayout.LayoutParams emptyViewParams = (RelativeLayout.LayoutParams) emptyView.getLayoutParams();
//        emptyViewParams.width = layoutParams.width;
//        emptyViewParams.height = (int) (sEmptyViewHeight * ratio);
//        emptyView.setLayoutParams(emptyViewParams);
//
//        nameView.setTextSize(sNameOringinalSize * ratio);
//        LinearLayout.LayoutParams descriptionParams = (LinearLayout.LayoutParams) desTV.getLayoutParams();
//        descriptionParams.width = sDescriptionWidth;
//        desTV.setLayoutParams(descriptionParams);
//        desTV.setTextSize(sDescriptionOriginalSize * ratio);
//
//        viewGroup.setTag(new Integer(funId));
//        viewGroup.setOnClickListener(this);
//        ret.addView(viewGroup, layoutParams);
//        return ret;
//    }
//
//    public List<Integer> getRecommendNew() {
//        List<Integer> allFeatures = new ArrayList<>();
//        List<Integer> retList = new ArrayList<>();
//
//        //区分识别岁数功能和其他功能
//
//            if (mReportType != BaseFaceResultBean.REPORT_TYPE_FACE_AGING &&
//                    SVipStrategyManager.getInstance().getCanShowFunction(SVipStrategyManager.FUNCTION_FACE_AGING)) {
//                allFeatures.add(BaseFaceResultBean.REPORT_TYPE_FACE_AGING);
//            }
//
//            if (mReportType != BaseFaceResultBean.REPORT_TYPE_EXOTIC &&
//                    SVipStrategyManager.getInstance().getCanShowFunction(SVipStrategyManager.FUNCTION_EXOTIC)) {
//                allFeatures.add(BaseFaceResultBean.REPORT_TYPE_EXOTIC);
//            }
//            if (mReportType != BaseFaceResultBean.REPORT_TYPE_GENDER_SWITCH &&
//                    SVipStrategyManager.getInstance().getCanShowFunction(SVipStrategyManager.FUNCTION_GENDER_SWITCH)) {
//                allFeatures.add(BaseFaceResultBean.REPORT_TYPE_GENDER_SWITCH);
//            }
//
//
//            //未使用的功能：按照优先级优先推荐
//            for (Integer funId : allFeatures) {
//                boolean use = SPDataManager.getFunIsUsed(funId);
//                if (!use) {
//                    if (retList.size() == 1 && funId == retList.get(0)) {
//                        continue;
//                    }
//                    retList.add(funId);
//                    if (retList.size() == 2) {
//                        return retList;
//                    }
//                }
//            }
//            //未使用功能不足2个：随机推荐
//            Random random = new Random();
//            for (int i = 0; i < allFeatures.size(); i++) {
//                int position = random.nextInt(allFeatures.size());
//                if (retList.size() == 1 && allFeatures.get(position) == retList.get(0)) {
//                    continue;
//                }
//                retList.add(allFeatures.get(position));
//                if (retList.size() == 2) {
//                    return retList;
//                }
//            }
//
//
//        return retList;
//    }

    /**
     * 功能使用后，保存sp
     */
    public void saveFunUsed() {
        SPDataManager.saveFunUseByFunId(mReportType);
    }

    /**
     * 展示过某个推荐功能后，需要保存sp
     */
    public void saveRecommendFunShow() {
        SPDataManager.saveLastRecommendByFunId(mReportType, mRecommendFunName);
    }

    public void onPageShow() {
        saveFunUsed();
        saveRecommendFunShow();
    }

}
